package com.fuelindustries.ant.utils;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.Arrays;
import java.util.List;

public class FileUtils 
{
	
	public static String getFileExtension( File aFile )
	{
		String filename = aFile.getAbsolutePath();
		
		String ext = filename.substring(filename.lastIndexOf('.')+1, filename.length());
		return( ext );
	}
	
	public static boolean deleteDirRecursive(File dir) 
	{
        if( dir.isDirectory() ) 
        {
            String[] children = dir.list();
            for( int i=0; i<children.length; i++ ) 
            {
                boolean success = deleteDirRecursive( new File( dir, children[ i ] ) );
                if( !success ) 
                {
                    return false;
                }
            }
        }
        return dir.delete();
    } 
	
	public static String getContents(File aFile) throws IOException 
	 {
		FileReader fr = new FileReader(aFile);
        BufferedReader br = new BufferedReader(fr);
                
        String eachLine = br.readLine();
        String exp = "";
        
        while(eachLine != null) 
        {
          exp += eachLine + "\n";
          eachLine = br.readLine();
        }
        
        fr.close();
        br.close();
	    
	    return exp;
	}
	
	public static void setContents(File aFile, String aContents) throws FileNotFoundException, IOException 
	{
		if (aFile == null) 
		{
			throw new IllegalArgumentException("File should not be null.");
		}
		
		if (!aFile.exists()) 
		{
			throw new FileNotFoundException ("File does not exist: " + aFile);
		}
		
		if (!aFile.isFile()) 
		{
			throw new IllegalArgumentException("Should not be a directory: " + aFile);
		}
		
		if (!aFile.canWrite()) 
		{
			throw new IllegalArgumentException("File cannot be written: " + aFile);
		}
		
		//use buffering
		Writer output = new BufferedWriter(new FileWriter(aFile));
		try 
		{
			//FileWriter always assumes default encoding is OK!
			output.write( aContents );
		}
		finally 
		{
			output.close();
		}
	}
	
	public static String createAbsolutePathFromRelative( String basepath, String relativepath )
	{
		String folderdelimeter = "/";
		
		relativepath = relativepath.replace( ":", folderdelimeter );

		String[] pathfolders = relativepath.split(folderdelimeter);
		String[] basefolders = basepath.split( "/" );
		
		List<String> v = Arrays.asList( pathfolders );
		List<String> b = Arrays.asList( basefolders );
		
		int foldercount = 0;
		int i;
		String filepath = "";
		
		for( i = 0; i<v.size(); i++ )
		{
			String dir = v.get(i);
			
			if( dir.equals( ".." ) )
			{
				foldercount++;
			}
			else if( dir.equals( "." ) )
			{

			}
			else
			{
				String fd = "/";
				
				if( i == v.size() - 1 )
				{
					fd = "";
				}
				filepath += dir + fd;
			}

			
		}
		
		String newpath = "";
		
		for( i = 0; i<b.size()-foldercount; i++ )
		{
			String f = b.get(i);
			newpath += f + "/";
		}
		
		return( newpath + filepath );
	}
	
}
